from flask import Blueprint,request,Flask,session
from utils.response_utils import ResponseUtils
from models.note_model import NoteModel
from service.note_service import NoteService
from application.note_comment_like_application import NoteCommentLikeApplication
from app.service.common_insert_service import CommonInsertSerevice
from service.oss_service import OssService
from app.service.img_service import ImgService
from app.service.search_category_service import SearchCategoryService

note = Blueprint('note', __name__)

@note.route('/get_notes', methods=['POST'])
def get_notes():
    try:
        current = request.get_json().get('current')
        page_size = request.get_json().get('page_size')
        res = NoteCommentLikeApplication.get_notes(current,page_size)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)
    return ResponseUtils.success(data=res)


@note.route('/get_notes_by_user_id', methods=['POST'])
def get_notes_by_user_id():
    try:
        user_id = request.get_json().get('user_id')
        current = request.get_json().get('current')
        res = NoteService.get_notes_by_user_id(user_id,current)
        return ResponseUtils.success(res)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)
    
@note.route('/insert_note', methods=['POST'])
def insert_note():
    try:
        print(session.get('user_id'))
        print('[]',request.get_json())
        data = request.get_json()
        # 新文章创建
        try:
            res = CommonInsertSerevice.insert_item_by_arg("note",request)
        except Exception as e:
            return ResponseUtils.error(message=str('CommonInsertSerevice.insert_item_by_arg',e), code=500)
        try:
            SearchCategoryService.insert_category_note(res['note_id'],data.get('category_name'),data.get('category_description'))
        except Exception as e:
            return ResponseUtils.error(message=str('SearchCategoryService.insert_category_note',e), code=500)
        images = data.get('images')
        print('images',images)
        counter = 0

        for image in images:
            print('image', image)
            file_url = image['file_url']
            slider_img_id = image['slider_img_id']
            
            # 使用计数器判断是否是第一个图片
            if counter == 0:
                try:
                    ImgService.update_note_index_img_path(res['note_id'], file_url)
                except Exception as e:
                    print('更新首页图片失败', e)
            
            try:
                print('res1', res)
                ImgService.update_slider_img_info_by_note_id(res['note_id'], slider_img_id)
            except Exception as e:
                print('1', e)
            
            # 递增计数器
            counter += 1
        request.get_json()['map_info_id'] = res['map_info_id']
        try:
            map_item = CommonInsertSerevice.insert_item_by_arg("map_info",request)
        except Exception as e:
            print('2', e)       
        request.get_json()['note_id'] = res['note_id']
        try:
            CommonInsertSerevice.insert_item_by_arg("collections",request)
        except Exception as e:
            print('3', e)
        return ResponseUtils.success(data=map_item)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

#  改note内容+轮播图
@note.route('/upload_note', methods=['POST'])
def upload_note():
    try:
        data = request.get_json()
        note_id = data.get('note_id')
        slide_imgs = data.get('slider_images')

        # 参数校验
        if not note_id or not slide_imgs:
            return ResponseUtils.error(data='Invalid note_id or slider_images', code=400)

        # 更新note内容
        upload_note_res = NoteService.update_note(request)

        counter = 0
        for slide_img in slide_imgs:
            slider_id = slide_img.get('slider_id')
            image_url = slide_img.get('image_url')

            # 如果 slider_id 或 file_url 为空，跳过本次循环
            if not slider_id or not image_url:
                continue

            # 更新轮播图图片地址
            upload_note_slider_res = ImgService.update_slider_img_info_by_note_id(note_id,slider_id)
    
            if counter == 0:
                # 更新note首页图片地址
                upload_note_index_photo_res = ImgService.update_note_index_img_path(note_id, image_url)

            counter += 1
        return ResponseUtils.success(data='更新成功',code=200)
    except Exception as e:
        return ResponseUtils.error(data=str(e), code=500)
    
@note.route('/delete_note', methods=['POST'])
def delete_note():
    try:
        res = NoteService.delete_note(request)
        return ResponseUtils.success(res)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)
    
# 不根据状态查询全部 note
@note.route('/get_all_notes', methods=['POST'])
def get_all_notes():
    try:
        current = request.get_json().get('current')
        page_size = request.get_json().get('page_size')
        res = NoteCommentLikeApplication.get_all_notes(current,page_size)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)
    return ResponseUtils.success(data=res)

# @note.route('/get_by_note_id/<string:note_id>', methods=['GET'])
# def get_by_note_id(note_id):
#     try:
#         # req = request.get_json()
#         # print(req)
#         res = NoteService.get_by_note_id(note_id)
#         return ResponseUtils.success(res)
#     except Exception as e:
#         return ResponseUtils.error(message=str(e), code=500)